package ${package}.controller;


import javax.servlet.http.HttpServletResponse
import ${package}.pojo.ExamplePOJO
import ${package}.view.ExampleManyComponentsView
import ${package}.view.ExampleView
import org.joda.time.DateTime
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.DisposableBean
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.servlet.ModelAndView
import br.com.me.ceap.dynamic.extension.service.CatalogManager
import javax.inject.Inject

/**
 * Example controller. It implements DisposableBean, although that is not stricly necessary.
 */
@Controller
@RequestMapping("/{customRootName}/do/example")
public class ExampleController implements DisposableBean {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Inject
    CatalogManager catalogManager;

    public ExampleController() {
        log.info("Constructing Example Controller, version 24.");
    }

    void destroy() {
        log.info("Destroying the Example controller on context shutdown, version 24.");
    }

    @RequestMapping(method = RequestMethod.GET)
    public void index(@PathVariable String customRootName, HttpServletResponse response) {
        log.info("Example hello world -- writing directly to response!");
        response.setContentType("text/html");
        response.getWriter().println("<a href=\"/" + customRootName + "/do/example/view\">See view example</a>");
    }

    @RequestMapping(value = "/view")
    public ModelAndView showWithView(@PathVariable String customRootName) {
        log.info("Example hello world with view!");

        // Some highly complicated calculations...
        DateTime today = DateTime.now();
        DateTime yesterday = today.minusDays(1);

        // ModelAndView is default Spring MVC return type.
        ModelAndView mav = new ModelAndView();

        // Create the view, demonstrating passing parameter in the constructor (typed 'firstValue' in the view)
        ExampleView view = new ExampleView(today);

        // Set the view in the ModelAnd View
        mav.setView(view);

        // Demonstrate passing parameter via model object (untyped)
        mav.addObject("secondValue", yesterday);

        // Another parameter to pass the customRootName to be able to link correctly from the view
        mav.addObject("customRootName", customRootName);

        // return the MaV
        return mav;
    }

    @RequestMapping(value = "/components")
    public ModelAndView showViewWithManyComponents(@PathVariable String customRootName) {
        log.info("Example hello world with view with a lot of components v26!");

        // This is a one-liner, as opposed to showWithView(), which is very verbose.
        return new ModelAndView(new ExampleManyComponentsView(customRootName, new ExamplePOJO()));
    }


    /*
    @RequestMapping(value = "/getSomeExampleGridJSONData", method = RequestMethod.POST)
    public ModelAndView getSomeExampleGridJSONData(@RequestParam(value = "GridFormat", required = false) Integer gridFormat) {

        // Create a fake data-source to be used by the view in rendering. This should come from a service, etc.
        List<ExamplePOJO> pojoList = new ArrayList<ExamplePOJO>();
        pojoList.add(new ExamplePOJO("Mr. One Example", "one's description", DateTime.now().minusYears(30), 190));
        pojoList.add(new ExamplePOJO("Ms. Two Example", "two's description", DateTime.now().minusYears(26), 170));

        return new GenericGridModelAndView(pojoList, gridFormat);
    }
    */

    @RequestMapping(value = "/catalog", method = RequestMethod.GET)
    public void catalog(@PathVariable String customRootName, HttpServletResponse response) {
        response.setContentType("text/html");
        response.getWriter().println("Locale by CatalogManager: " + catalogManager.getLocale() +
                "<br> test.message: " + catalogManager.getMessage("test.message"));
    }

}
